package com.yc.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.bean.UserTestRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserTestRecordDao extends BaseMapper<UserTestRecord> {
    
    /**
     * 查询用户是否已有该测评的记录
     */
    @Select("SELECT * FROM userTestRecords " +
            "WHERE userId = #{userId} AND testId = #{testId} " +
            "ORDER BY createTime DESC LIMIT 1")
    UserTestRecord selectLatestRecord(@Param("userId") Long userId, @Param("testId") Long testId);
    
    /**
     * 分页查询用户测评历史
     */
    @Select("SELECT utr.*, pt.title as testTitle, pt.icon as testIcon " +
            "FROM userTestRecords utr " +
            "INNER JOIN psychologicalTests pt ON utr.testId = pt.testId " +
            "WHERE utr.userId = #{userId} " +
            "ORDER BY utr.createTime DESC")
    IPage<UserTestRecord> selectUserTestHistory(Page<?> page, @Param("userId") Long userId);
} 